package com.egoo.ticket.server.utils.command;

import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.dao.WorkOrderFilterMenuDao;
import com.egoo.ticket.server.pojo.entity.WorkOrderFilterMenu;
import com.egoo.ticket.server.utils.common.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
@Order(value = 1)
public class sysFilterMenuExceute implements CommandLineRunner {
	public final static String dbType = "oracle";
	@Value("${spring.pagehelper.helper-dialect}")
	String reallDbType;
	@Autowired
	WorkOrderFilterMenuDao workOrderFilterMenuDao;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void run(String... args) throws Exception {
		//如果是mysql不用此方法进行系统过滤器数据铺地
		if(!dbType.equals(reallDbType)){
			return;
		}
		List<WorkOrderFilterMenu> aDefault = workOrderFilterMenuDao.selectSysFilterMenu(0L, "default");
		if (!CollectionUtils.isEmpty(aDefault) && aDefault.size() == 7){
			return;
		}
		JSONObject allListFilterCondition = CommonUtil.readDbData("/oracledbjsondata/allListFilterCondition.json");
		JSONObject myCreateFilterCondition = CommonUtil.readDbData("/oracledbjsondata/myCreateFilterCondition.json");
		JSONObject editFilterCondition = CommonUtil.readDbData("/oracledbjsondata/editFilterCondition.json");
		JSONObject myMonitorFilterCondition = CommonUtil.readDbData("/oracledbjsondata/myMonitorFilterCondition.json");
		JSONObject handlingFilterCondition = CommonUtil.readDbData("/oracledbjsondata/handlingFilterCondition.json");
		JSONObject myComplateFilterCondition = CommonUtil.readDbData("/oracledbjsondata/myComplateFilterCondition.json");
		JSONObject myDeptFilterCondition = CommonUtil.readDbData("/oracledbjsondata/myDeptFilterCondition.json");
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		WorkOrderFilterMenu allListWorkOrderFilterMenu =
				new WorkOrderFilterMenu(1,"所有工单列表","所有工单列表", 0L,"无",1,1,"系统内置",1,timestamp,"0","系统","0","系统",timestamp,allListFilterCondition,"all","","all","",null,"default",null,null);
		WorkOrderFilterMenu myCreateWorkOrderFilterMenu =
				new WorkOrderFilterMenu(2,"我创建的工单","我创建的工单", 0L,"无",1,1,"系统内置",2,timestamp,"0","系统","0","系统",timestamp,myCreateFilterCondition,"all","","all","",null,"default",null,null);
		WorkOrderFilterMenu editWorkOrderFilterMenu =
				new WorkOrderFilterMenu(3,"编辑中工单","编辑中工单", 0L,"无",1,1,"系统内置",3,timestamp,"0","系统","0","系统",timestamp,editFilterCondition,"all","","all","",null,"default",null,null);
		WorkOrderFilterMenu myMonitorWorkOrderFilterMenu =
				new WorkOrderFilterMenu(4,"我关注的工单","我关注的工单", 0L,"无",1,1,"系统内置",4,timestamp,"0","系统","0","系统",timestamp,myMonitorFilterCondition,"all","","all","",null,"default",null,null);
		WorkOrderFilterMenu handlingWorkOrderFilterMenu =
				new WorkOrderFilterMenu(5,"待处理工单","待处理工单", 0L,"无",1,1,"系统内置",5,timestamp,"0","系统","0","系统",timestamp,handlingFilterCondition,"all","","all","",null,"default",null,null);
		WorkOrderFilterMenu myComplateWorkOrderFilterMenu =
				new WorkOrderFilterMenu(6,"我完成的工单","我完成的工单", 0L,"无",1,1,"系统内置",6,timestamp,"0","系统","0","系统",timestamp,myComplateFilterCondition,"all","","all","",null,"default",null,null);
		WorkOrderFilterMenu myDeptWorkOrderFilterMenu =
				new WorkOrderFilterMenu(7,"我部门下的工单","我部门下的工单", 0L,"无",1,1,"系统内置",7,timestamp,"0","系统","0","系统",timestamp,myDeptFilterCondition,"all","","all","",null,"default",null,null);

		ArrayList<WorkOrderFilterMenu> workOrderFilterMenus = new ArrayList<>();
		workOrderFilterMenus.add(allListWorkOrderFilterMenu);
		workOrderFilterMenus.add(myCreateWorkOrderFilterMenu);
		workOrderFilterMenus.add(editWorkOrderFilterMenu);
		workOrderFilterMenus.add(myMonitorWorkOrderFilterMenu);
		workOrderFilterMenus.add(handlingWorkOrderFilterMenu);
		workOrderFilterMenus.add(myComplateWorkOrderFilterMenu);
		workOrderFilterMenus.add(myDeptWorkOrderFilterMenu);
//		workOrderFilterMenuDao.insertByBatch(workOrderFilterMenus);

		workOrderFilterMenus.forEach(v ->{
			workOrderFilterMenuDao.insertMenu(v);
		});
	}
}
